﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Application.App;
using AutoMapper;
using Domain.Model;
using DTO;
using FacadeApi.Extension;
using Infrastructure.Error;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System.IO;

namespace FacadeApi.Controllers
{

    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    [ProducesResponseType(401)]
    [ProducesResponseType(403)]
    public class UserController : ControllerBase
    {
        public UserApplication userApplication { get; set; }
        public ILogger<UserController> _logger { get; set; }
        /// <summary>
        /// 登录注册  DeviceToken
        /// </summary>
        /// <param name="userLoginInput"></param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpPost]
        [TokenValidFilter]
        public ActionResult Login(UserLoginInput userLoginInput)
        {
            string ipaddress = this.HttpContext.Connection.RemoteIpAddress.ToString();
            try
            {
                userLoginInput.IP = ipaddress;
                var token = userApplication.Login(userLoginInput);

                return this.Success(token);
            }
            catch (NotRegisterException ex)
            {
                return this.DefaultResult(ResultCode.NotRegister,ex.data, ex.Message);
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }

        }

        [AllowAnonymous]
        [HttpPost]
        [TokenValidFilter]
        public ActionResult<UserLoginOutput> Register(RegisterInput registerInput)
        {
            try
            {
                var token = userApplication.UserRegister(registerInput);

                return this.Success(token);
            }
            catch (NotRegisterException ex)
            {
                return this.DefaultResult(ResultCode.NotRegister, ex.data, ex.Message);
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }

        }

        /// <summary>
        /// 刷新Token/刷新信息   DeviceToken
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpPut]
        [TokenValidFilter]
        public ActionResult<UserLoginOutput> Refresh([FromForm]string token)
        {
            try
            {
                var newtoken = userApplication.Refresh(token);

                return this.Success(newtoken);
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }

        }

        /// <summary>
        /// 微信绑定
        /// </summary>
        /// <param name="userBindWxInput"></param>
        /// <returns></returns>
        [HttpPost, TokenValidFilter]
        public ActionResult BindWx(UserBindWxInput userBindWxInput)
        {
            if (string.IsNullOrEmpty(userBindWxInput.WeixinOpenId)) return this.FailResult(null, "微信OpenId不允许为空！");

            try
            {
                var uid = User.Claims.First(i => i.Type == "ID").Value;

                var r = userApplication.BindWx(uid,userBindWxInput);

                return this.Success(r);
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }

        }

        /// <summary>
        /// 人脸认证获取FaceID
        /// </summary>
        /// <param name="realNameAuthInput"></param>
        /// <returns></returns>
        [HttpPost, TokenValidFilter]
        public ActionResult<NewFaceInfoOutput> NewFaceInfo(NewFaceInfoInput realNameAuthInput)
        {
            if (string.IsNullOrWhiteSpace(realNameAuthInput.RealName)) return this.FailResult(null, "身份证姓名未填写!");
            if (string.IsNullOrWhiteSpace(realNameAuthInput.CardNO)) return this.FailResult(null, "身份证号码未填写!");

            try
            {
                var uid = User.Claims.First(i => i.Type == "ID").Value;
                var r=userApplication.NewFaceInfo(uid, realNameAuthInput);

                return this.Success(r);
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }

        }
        /// <summary>
        /// 人脸认证结果验证
        /// </summary>
        /// <param name="orderNo"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult<NewFaceInfoOutput> GetFaceResult(string orderNo)
        {
            try
            {
                var uid = User.Claims.First(i => i.Type == "ID").Value;
                var r = userApplication.GetFaceResult(uid, orderNo);

                return this.Success(r);
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }
        }

        /// <summary>
        /// 获取实名认证信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult<RealNameInfo> GetRealNameAuthInfo()
        {
            try
            {
                var uid = User.Claims.First(i => i.Type == "ID").Value;
                var r = userApplication.GetRealNameAuthInfo(int.Parse(uid));
                if (r == null) return this.FailResult(null,"没有认证信息");

                return this.Success(r);
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }

        }
        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult<UserInfoOutput> GetUserInfo()
        {
            try
            {
                var uid = User.Claims.First(i => i.Type == "ID").Value;

                var r = userApplication.GetUserInfo(int.Parse(uid));

                return this.Success(r);
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }
        }

        [HttpPost, TokenValidFilter]
        public ActionResult SetUserInfo(UserInfoInput userInfoInput)
        {
            try
            {
                var uid = User.Claims.First(i => i.Type == "ID").Value;

                var r = userApplication.SetUserInfo(int.Parse(uid), userInfoInput);

                return this.Success(r);
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }
        }

    }
}
